<template>
  <div class="app-container">
    <el-form
      label-position="left"
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="政策类型" prop="type">
        <el-select
          v-model="queryParams.typeName"
          placeholder="请选择政策类型"
          clearable
          @keyup.enter.native="handleQuery"
        >
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="标题" prop="titile">
        <el-input
          v-model="queryParams.titile"
          placeholder="请输入标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发布者" prop="publisher">
        <el-input
          v-model="queryParams.publisher"
          placeholder="请输入发布者"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="关键字" prop="keyword">
        <el-input
          v-model="queryParams.keyword"
          placeholder="请输入关键字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="日期"
        ><el-date-picker
          v-model="queryParams.searchTime"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
          align="center"
          @keyup.enter.native="handleFuzzyQuery"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleFuzzyQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:policy:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="policyList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="政策类型"
        align="center"
        prop="typeName"
        show-overflow-tooltip="true"
      />
      <el-table-column
        label="标题"
        align="center"
        prop="titile"
        show-overflow-tooltip="true"
      />
      <!-- <el-table-column
        label="发布者"
        align="center"
        prop="publisher"
        show-overflow-tooltip="true"
      /> -->
      <el-table-column
        label="公布日期"
        align="center"
        prop="publishTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.publishTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="关键字"
        align="center"
        prop="keyword"
        show-overflow-tooltip="true"
      />

      <el-table-column
        label="内容预览"
        align="center"
        prop="content"
        show-overflow-tooltip="true"
      />

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="viewDetail(scope.row)"
            v-hasPermi="['system:policy:query']"
            >查看详情</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 公告详情对话框 -->
    <el-dialog
      title="详情信息"
      :visible.sync="openDetail"
      width="80%"
      append-to-body
    >
      <el-table
        :data="currentPolicy"
        v-loading="loading"
        element-loading-text="拼命加载中"
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(0, 0, 0, 0.8)"
      >
        <!-- <el-table-column label="发布者" align="center" prop="publisher" />
        <el-table-column label="标题" align="center" prop="titile" /> -->
        <el-table-column label="内容" align="center" prop="content" />
        <!-- <el-table-column label="关键字" align="center" prop="keyword" />

        <el-table-column
          label="公布日期"
          align="center"
          prop="publishTime"
          width="180"
        >
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.publishTime, "{y}-{m}-{d}") }}</span>
          </template>
        </el-table-column> -->
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import {
  listPolicy,
  getPolicy,
  delPolicy,
  addPolicy,
  updatePolicy,
  fuzzyListPolicy,
} from "@/api/system/policy";

export default {
  name: "Policy",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 政策信息表格数据
      policyList: [],
      //当前行政策信息
      currentPolicy: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否展示详情记录
      openDetail: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        publisher: null,
        titile: null,
        content: null,
        keyword: null,
        typeName: null,
        publishTime: null,
        status: null,
        searchTime: [],
        beginDate: null,
        endDate: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      // 公告类型参数
      options: [
        {
          label: "环保相关",
          value: "环保相关",
        },
        {
          label: "其他方面",
          value: "其他方面",
        },
      ],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询政策信息列表 */
    getList() {
      this.loading = true;
      listPolicy(this.queryParams).then((response) => {
        this.policyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 按条件模糊搜索政策列表 */
    getFuzzyList() {
      this.loading = true;
      fuzzyListPolicy(this.queryParams).then((response) => {
        this.policyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        policyId: null,
        publisher: null,
        titile: null,
        content: null,
        keyword: null,
        typeName: null,
        publishTime: null,
        status: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 模糊搜索按钮操作 */
    handleFuzzyQuery() {
      this.queryParams.pageNum = 1;
      if (this.queryParams.searchTime.length != 0) {
        this.queryParams.beginDate = this.queryParams.searchTime[0];
        this.queryParams.endDate = this.queryParams.searchTime[1];
      }
      this.getFuzzyList();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.policyId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加政策信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const policyId = row.policyId || this.ids;
      getPolicy(policyId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改政策信息";
      });
    },
    viewDetail(row) {
      this.openDetail = true;
      this.currentPolicy[0] = row;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.policyId != null) {
            updatePolicy(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPolicy(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const policyIds = row.policyId || this.ids;
      this.$modal
        .confirm('是否确认删除政策信息编号为"' + policyIds + '"的数据项？')
        .then(function () {
          return delPolicy(policyIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/policy/export",
        {
          ...this.queryParams,
        },
        `policy_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
